#include "DEHT/deht.h"
#include "global/misc.h"
#include <string.h>
#include "global/global_rainbow_table.h"
#include "exhaustive_search/dictionary.h"
#include "exhaustive_search/rule.h"

DEHT* make_ht(int close, int call_read, int call_calc);
void query_ht(DEHT* d);
DEHT* make_small_ht(int close, int call_read, int call_calc);
void test_insert_uniquely();



int main() {
	String* rule, *word = 0;
	int i = 0, n;
	the_dictionary = create_dictionary_from_file("tests/dictionary_example.txt");
	rule = make_string_from_char_arr("@&1@&2");
	the_rule = make_rule_list_from_string(*rule);
	n = count_words_in_whole_rule(the_rule, the_dictionary);
	word = make_empty_string();
	printf("num of things in rule: %d\n", n);
	for (i = 0; i < n; i++) {
		word_k_for_rule(i, word);
		fwrite(word->data, 1, word->size, stdout);
		printf("\n");
		String_reset(word);
	}

	return 0;
}

DEHT* make_ht(int close, int call_read, int call_calc) {
	int i = 0, size1, size2;
	DEHT* d;
	char buf1[100] = {0}, buf2[100] = {0};
	d = create_empty_DEHT("rofl", str_mod, keyTo64Bit, "MD5", 0, 7, 8, 0);

	if (call_read) {
		read_DEHT_pointers_table(d);
	}

	if (call_calc) {
		calc_DEHT_last_block_per_bucket(d);
	}

	for (i = 0; i < 70144; i++) {
		if (i == 100)
			printf("ROFL");
		sprintf(buf1, "%d", i);
		sprintf(buf2, "%d", i+17);
		size1 = strlen(buf1);
		size2 = strlen(buf2);
		add_DEHT(d, (unsigned char*)buf1, size1, (unsigned char*)buf2, size2);
		if (i % 1000 == 0) {
			printf("%d\n", i);
		}
	}
	if (close) {
		lock_DEHT_files(d);
	}
	return d;
}

DEHT* make_small_ht(int close, int call_read, int call_calc) {
	DEHT* d;
	int i, size1, size2;
	char buf1[100] = {0}, buf2[100] = {0};
	d = create_empty_DEHT("rofl", str_mod, keyTo64Bit, "MD5", 0, 7, 8, 0);

	if (call_read) {
		read_DEHT_pointers_table(d);
	}

	if (call_calc) {
		calc_DEHT_last_block_per_bucket(d);
	}

	for (i = 0; i < 20; i++) {
		sprintf(buf1, "%d", i*100 + 17);
		sprintf(buf2, "%d", i);
		size1 = strlen(buf1);
		size2 = strlen(buf2);
		add_DEHT(d, (unsigned char*)buf1, size1, (unsigned char*)buf2, size2);
	}


	if (close) {
		lock_DEHT_files(d);
	}
	return d;
}

void test_insert_uniquely() {
	unsigned char buffer[20] = {0};
	int i;
	DEHT* d = create_empty_DEHT("rofl", str_mod, keyTo64Bit, "MD5", 0, 7, 8, 0);
	add_DEHT(d, (unsigned char*)"rofl", 4, (unsigned char*)"mao", 3);
	insert_uniquely_DEHT(d, (unsigned char*)"rofl", 4, (unsigned char*)"hahaha", 6);
	i = query_DEHT(d, (unsigned char*)"rofl", 4, buffer, 20);
	printf("data: %s\n%d bytes", buffer, i);
}

